from time import sleep
#from selenium import webdriver
import tkinter as tk
import sys
import subprocess
from urllib import request,parse

with open("./buffer","r") as f:
    global x,y,w,h
    for line in f:
        line2=line.strip()
        pos=line2.find('=')
        if pos==-1:
            sys.exit()

        name, value = line2.split('=')
        if name=="x":
            x=int(value)
        elif name=="y":
            y=int(value)
        elif name=="width":
            w=int(value)
        elif name=="height":
            h=int(value)


root=tk.Tk()

scx = root.winfo_screenwidth()  # 获取当前屏幕的宽
scy = root.winfo_screenheight()  # 获取当前屏幕的高

root.geometry(f"+{x+int(w//3.5)}+{y+h}")

root.attributes("-alpha", 1)  # 透明度(0.0~1.0)
root.attributes("-toolwindow", True)  # 置为工具窗口(没有最大最小按钮)
root.attributes("-topmost", True)  # 永远处于顶层
root.overrideredirect(True)  # 去除边框
L1 = tk.Label(root, text="搜索")
L1.pack( side = tk.LEFT)
E1 = tk.Entry(root, bd =0)
E1.pack(side = tk.RIGHT)
E1.focus_set()
def quitt(event):
    root.quit()
    root.destroy()
    sys.exit()

def get_content(event):
    global content
    content=E1.get()
    root.quit()
    root.destroy()

root.bind("<Return>",get_content)
root.bind("<FocusOut>",quitt)
root.mainloop()


content_p=parse.quote(content)
#content_p=content.replace(' ','+')
print(content_p)
subprocess.Popen(f"msedge https://cn.bing.com/search?q={content_p}",shell=False)

"""driver = webdriver.Edge()

driver.get(r'https://www.bing.com/')
internation=driver.find_element_by_id("est_en")
internation.click()

element = driver.find_element_by_id("sb_form_q")
element.send_keys(content)  # 模拟键盘输入 send_keys
ele = driver.find_element_by_id("search_icon")
ele.click()  # 鼠标左键单击搜索按钮"""


